package com.zhao.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhao.pojo.Tags;
import com.zhao.pojo.User;
import com.zhao.pojo.Wallet;
import com.zhao.pojo.result.Response;
import com.zhao.pojo.result.ResponseResult;
import com.zhao.service.TagsService;
import com.zhao.service.WalletService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author : Mr.zhao
 * @Date: 2022/05/28/20:53
 * @Description:
 */
@RestController
@RequestMapping("tags")
public class TagsController {
    @Autowired
    TagsService tagsService;
    @Autowired
    WalletService walletService;

    @RequestMapping("queryTags")
    public ResponseResult queryTags(@RequestParam(required = false)String search){
        QueryWrapper<Tags> wrapper=new QueryWrapper<>();
        if (search!=null){
            wrapper.like("name",search);
        }
        List<Tags> list = tagsService.list(wrapper);
        return Response.makeOKRspData(list);
    }
    @RequestMapping("addTags")
    public ResponseResult addTags(Tags tags){
        User loginUser = (User) SecurityUtils.getSubject().getPrincipal();
        Wallet byId = walletService.getById(loginUser.getUserId());
        String grade = byId.getGrade();
        grade = grade.replace("L", "");
        int i = Integer.parseInt(grade);
        if (i<=3){
            return Response.makeErrRsp("等级小于3L无法支付");
        }
        tagsService.save(tags);
        return Response.makeOKRsp();
    }
}
